package com.efounder.view;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.efounder.mobilemanager.R;
import com.utilcode.util.ScreenUtils;
import com.utilcode.util.SizeUtils;

/**
 * 自定升级弹框dialog 其他功能请勿使用
 */
public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int theme) {
        super(context, theme);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window dialogWindow = getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.width =  WindowManager.LayoutParams.MATCH_PARENT;
        lp.height =  WindowManager.LayoutParams.WRAP_CONTENT;

        dialogWindow.setAttributes(lp);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String message;
        private String positiveButtonText;
        private String negativeButtonText;
        private ProgressBar progressBar;
        private TextView progressTV;
        private int progress;
        private boolean cancelable;
        private boolean canceledOnTouchOutside;
        private CustomDialog dialog;
        private TextView positiveButton;
        private TextView negativeButton;
        private OnClickListener positiveButtonClickListener;
        private OnClickListener negativeButtonClickListener;

        public Builder(Context context) {
            this.context = context;
        }

        public CustomDialog getDialog() {
            return dialog;
        }

        public TextView getPositiveButton() {
            return positiveButton;
        }

        public TextView getNegativeButton() {
            return negativeButton;
        }

        public void setProgress(int progress) {
            progressBar.setProgress(progress);
            progressTV.setText((int) progress + "%");
        }

        public Builder setCancelable(boolean cancelable) {
            this.cancelable = cancelable;
            return this;
        }

        public Builder setCanceledOnTouchOutside(boolean canceledOnTouchOutside) {
            this.canceledOnTouchOutside = canceledOnTouchOutside;
            return this;
        }

        public Builder setMessage(String message) {
            this.message = message;
            return this;
        }

        /**
         * Set the Dialog message from resource
         *
         * @return
         */
        public Builder setMessage(int message) {
            this.message = (String) context.getText(message);
            return this;
        }

        /**
         * Set the Dialog title from resource
         *
         * @param title
         * @return
         */
        public Builder setTitle(int title) {
            this.title = (String) context.getText(title);
            return this;
        }

        /**
         * Set the Dialog title from String
         *
         * @param title
         * @return
         */

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        /**
         * Set the positive button resource and it's listener
         *
         * @param positiveButtonText
         * @return
         */
        public Builder setPositiveButton(int positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = (String) context
                    .getText(positiveButtonText);
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setPositiveButton(String positiveButtonText,
                                         OnClickListener listener) {
            this.positiveButtonText = positiveButtonText;
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(int negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = (String) context
                    .getText(negativeButtonText);
            this.negativeButtonClickListener = listener;
            return this;
        }

        public Builder setNegativeButton(String negativeButtonText,
                                         OnClickListener listener) {
            this.negativeButtonText = negativeButtonText;
            this.negativeButtonClickListener = listener;
            return this;
        }

        public CustomDialog create() {
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            // instantiate the dialog with the custom Theme
            dialog = new CustomDialog(context, R.style.withwaitingupdate_loadingData_dialog);
            View layout = inflater.inflate(R.layout.progressdialog4update, null);
            progressBar = (ProgressBar) layout.findViewById(R.id.withwaitingupdate_progressbar);
            progressTV = (TextView) layout.findViewById(R.id.progresstext);


//            dialog.setContentView(layout);
//            setSize(dialog);
            dialog.setCancelable(cancelable);
            dialog.setCanceledOnTouchOutside(canceledOnTouchOutside);
            // set the dialog title
            ((TextView) layout.findViewById(R.id.withwaitingupdat_title)).setText(title);
            // set the confirm button
            if (positiveButtonText != null) {
                positiveButton = ((TextView) layout.findViewById(R.id.withwaitingupdate_down));
                positiveButton.setText(positiveButtonText);
                if (positiveButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.withwaitingupdate_down))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    positiveButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_POSITIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.withwaitingupdate_down).setVisibility(
                        View.GONE);
            }
            // set the cancel button
            if (negativeButtonText != null) {
                negativeButton = ((TextView) layout.findViewById(R.id.withwaitingupdate_cancel));
                negativeButton.setText(negativeButtonText);
                if (negativeButtonClickListener != null) {
                    ((TextView) layout.findViewById(R.id.withwaitingupdate_cancel))
                            .setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    negativeButtonClickListener.onClick(dialog,
                                            DialogInterface.BUTTON_NEGATIVE);
                                }
                            });
                }
            } else {
                // if no confirm button just set the visibility to GONE
                layout.findViewById(R.id.withwaitingupdate_cancel).setVisibility(
                        View.GONE);
            }
            // set the content message
            if (message != null) {
                ((TextView) layout.findViewById(R.id.withwaitingupdat_content)).setText(message);
            }

            dialog.setContentView(layout);
            //setSize(dialog);
            return dialog;
        }

        private LayoutParams setSize(CustomDialog dialog) {
            //设置dialog的大小
            Window dialogWindow = dialog.getWindow();
            WindowManager.LayoutParams lp = dialogWindow.getAttributes();

            int screenWidth = ScreenUtils.getScreenWidth();
//            lp.width = (int) (screenWidth * 0.85);
            lp.height = WindowManager.LayoutParams.WRAP_CONTENT;

            dialogWindow.getDecorView().setPadding(SizeUtils.dp2px(30), 0, SizeUtils.dp2px(30), 0);

            dialogWindow.setAttributes(lp);
            return lp;
        }
    }

}